<template>
<div>
  <div v-loading="loading">
    <div id="head">
      <el-link @click="$router.back()">返回</el-link>
      <el-tag type="info" style="margin-left: 20px">{{article.tagName}}</el-tag>
      <div class="title">{{article.title}}</div>
      <div class="user-info">
        <img  width="50px" height="50px" :src="article.headUrl"/><br/>{{article.username}}
      </div>
      <div class="topic-info">
        <div class="createTime">{{article.createTime}}</div>
        <div class="likes"><i class="el-icon-thumb"/> {{article.likes}} </div>
        <div class="comments"><i class="el-icon-chat-line-round"/> {{article.commentNum}} </div>
      </div>
    </div>
    <div id="body" v-html="article.content"></div>
    <el-button type="danger"  v-if="canLike && userId != ''" @click="like">点赞</el-button>
    <el-button type="info" :disable="loading" v-if="!canLike && userId != ''" @click="disLike">取消点赞</el-button>
    <el-button type="primary" :disable="loading" v-if="userId != ''" @click="isComment = true; commentParentId=0">评论</el-button>

  </div>

  <el-empty description="暂无评论" v-if="comments.length===0"/>
  <div id="comment" v-if="comments.length>0">
    <div id="commentList">
      <div class="commentItem" v-for="item in comments" style="clear: both">
        <img :src="item.headUrl" class="headImg"/><span class="username">{{item.username}}：</span>
        <span class="time">评论时间：{{item.createTime}}</span><br>
        <div class="content">{{item.content}}</div>
      </div>
    </div>
  </div>

  <el-dialog :visible.sync="isComment" title="评论">
    <el-form>
      <el-input style="margin-top: 30px" v-model="commentContent"/>
      <el-form-item style="margin-top: 30px">
        <el-button @click="makeComment" type="primary">提交</el-button>
        <el-button @click="isComment = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script>

export default {
  name: "ArticleItem",
  data(){
    return{
      id: '',
      article: {},
      loading: false,
      userId : '',
      canLike: false,
      commentContent: '',
      isComment: false,
      commentParentId: 0,
      comments: []
    }
  },
  methods:{
    like(){
      let param = {
        userId: this.userId,
        parentId: this.id,
        parent: 0
      }
      this.loading = true
      this.getRequest('article/like',param).then(resp=>{
        if (resp.status == 200) {
          this.article.likes += 1
          this.canLike=false
        }
      })
      this.loading = false
    },
    disLike(){

      let param = {
        userId: this.userId,
        articleId: this.id
      }
      this.loading = true
      this.getRequest('article/disLike',param).then(resp=>{
        if (resp.status == 200) {
          this.article.likes -= 1
          this.canLike=true
        }
      })
      this.loading = false
    },
    makeComment(){
      let param = {
        userId : this.userId,
        articleId : this.id,
        content : this.commentContent
      }
      this.loading = true
      this.postRequest('comment/', param).then(()=>{
        this.getComment()
        this.isComment = false
      })
      this.loading = false
    },
    getComment(){
      this.getRequest('comment/',{articleId:this.id}).then(resp => {
        this.comments = resp.data
        console.log(resp)
      })
    }
  },
  created() {
    let user = JSON.parse(sessionStorage.getItem("user"));
    if (user) {
      this.userId = user.id
    }

    this.id = this.$route.params.id
    this.getRequest('article/byId?id='+this.id).then(resp=>{
      this.article = resp.data
    })

    let param = {
      userId : this.userId,
      articleId : this.id
    }
    this.getRequest('article/canLike',param).then(resp => {
      this.canLike = resp.data
    })

    this.getComment()
  }
}
</script>

<style scoped>
.title{
  color: #333;
  font-size: 24px;
  border: none;
  line-height: 34px;
  margin: 20px 0 18px 0;
}
.topic-info{
  color: #ccc;
  font-size: 14px;
}
.createTime{
  float: left;
}
.likes{
  float: left;
  margin-left: 30px;
}
.comments{
  margin-left: 10px;
  float: left;
}
.topic-info{
  height: 25px;
  border-bottom: 1px solid #cccccc;
}
.user-info{
  width: 200px;
  text-align: center;
  position: absolute;
  right: 100px;
  top: 30px;
}
#head{
  position: relative;
}
#body{
  clear: both;
  margin: 20px 0;
  padding-bottom: 20px;
  border-bottom: 1px solid #cccccc;
}
#comment{
  margin-top: 30px;
  height: auto;
}
.commentItem{
  margin-top: 20px;
  padding-bottom: 60px;
  border-bottom: 1px solid #000;
}
.headImg{
  width: 50px;
  height: 50px;
  display: inline-block;
  float: left;
}
.username{
  float: left;
  font-size: 12px;
  margin-left: 20px;
}
.content{
  display: block;
  margin-left: 20px;
  margin-top: 10px;
  float: left;
}
.time{
  font-size: 12px;
  float: right;
  margin-right: 100px;
}
</style>